﻿using System;
using System.Configuration;
using System.Web.Configuration;

namespace Framework
{
    public class ConfigHelper
    {
        private static Configuration _config;

        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="path">配置文件地址</param>
        /// <param name="node">App节点名</param>
        /// <returns></returns>
        public static AppSettingsSection Read(string path, string node)
        {
            _config = WebConfigurationManager.OpenWebConfiguration(path);
            return (AppSettingsSection)_config.GetSection(node);
        }

        /// <summary>
        /// 添加一个Add节点
        /// </summary>
        /// <param name="path">配置文件地址</param>
        /// <param name="node">App节点名</param>
        /// <param name="key">节点名</param>
        /// <param name="value">节点值</param>
        /// <returns></returns>
        public static bool Adds(string path, string node, string key, string value)
        {
            AppSettingsSection appSettings = Read(path, node);
            try
            {
                appSettings.Settings.Add(key, value);
                _config.Save(ConfigurationSaveMode.Modified);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一个Add节点
        /// </summary>
        /// <param name="path">配置文件地址</param>
        /// <param name="node">App节点名</param>
        /// <param name="key">节点名</param>
        /// <param name="value">节点值</param>
        /// <returns></returns>
        public static bool Update(string path, string node, string key, string value)
        {
            AppSettingsSection appSettings = Read(path, node);
            try
            {
                appSettings.Settings[key].Value = value;
                _config.Save(ConfigurationSaveMode.Modified);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一个Add节点
        /// </summary>
        /// <param name="path">配置文件地址</param>
        /// <param name="node">App节点名</param>
        /// <param name="key">节点名</param>
        /// <returns></returns>
        public static bool Remove(string path, string node, string key)
        {
            AppSettingsSection appSettings = Read(path, node);
            try
            {
                appSettings.Settings.Remove(key);
                _config.Save(ConfigurationSaveMode.Modified);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 得到AppSettings中的配置字符串信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfigString(string key)
        {
            string cacheKey = "AppSettings-" + key;
            object objModel = DataCache.GetCache(cacheKey);
            if (objModel == null)
            {

                objModel = ConfigurationManager.AppSettings[key];
                if (objModel != null)
                {
                    DataCache.SetCache(cacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
                    return objModel.ToString();
                }
                return string.Empty;
            }
            return objModel.ToString();
        }

        /// <summary>
        /// 得到AppSettings中的配置Bool信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool GetConfigBool(string key)
        {
            bool result = false;
            string cfgVal = GetConfigString(key);
            if (!string.IsNullOrEmpty(cfgVal))
            {
                try
                {
                    result = bool.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    // Ignore format exceptions.
                }
            }
            return result;
        }

        /// <summary>
        /// 得到AppSettings中的配置Decimal信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static decimal GetConfigDecimal(string key)
        {
            decimal result = 0;
            string cfgVal = GetConfigString(key);
            if (!string.IsNullOrEmpty(cfgVal))
            {
                try
                {
                    result = decimal.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    // Ignore format exceptions.
                }
            }
            return result;
        }

        /// <summary>
        /// 得到AppSettings中的配置int信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetConfigInt(string key)
        {
            int result = 0;
            string cfgVal = GetConfigString(key);
            if (!string.IsNullOrEmpty(cfgVal))
            {
                try
                {
                    result = int.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    // Ignore format exceptions.
                }
            }
            return result;
        }
    }
}